package com.MonkWorks.timedormitory;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.MonkWorks.clockDTO.ClockActivity;
import com.MonkWorks.clockDTO.Job;
import com.MonkWorks.clockDTO.User;
import com.MonkWorks.timedormitoryDB.ItimeProvider;
import com.MonkWorks.timedormitoryDB.TimeDB;

public class SummaryActivity extends Activity {

	private TextView title;
	private TextView line1;
	private TextView line2;
	private TextView line3;
	private ListView activity;
	private int activityId;
	private String username;
	private Button button_Back;
	private String jobID;
	private int clockAction;
	private String time;
	private double Long;
	private double Lat;
	private String jobDesc;
	private String jobAdd;
	private String owner;
	private String ownerFirst;
	private String ownerLast;
	private String createdBy;
	private List<String> activities;
	private String JobValue;
	// call the database interface
	ItimeProvider timeDB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_summary);

		initComponent();

	}

	private void initComponent() {

		// add the database class
		timeDB = new TimeDB(this);
		getExtras();
		activitySummary();
		JobInfo();
		UserInfo();
		// find activity view items
		title = (TextView) findViewById(R.id.SummaryTitle);
		line1 = (TextView) findViewById(R.id.SummaryLine1);
		line2 = (TextView) findViewById(R.id.SummaryLine2);
		line3 = (TextView) findViewById(R.id.SummaryLine3);

		// Set the title to the seleted Job
		title.setText(jobID);
		// set line 1 with job address
		line1.setText(jobAdd);
		// set line 2 with job description
		line2.setText(jobDesc);
		// set line 3 with the user who created the job
		line3.setText(createdBy);

		// call and create a list view
		activity = (ListView) findViewById(R.id.SummaryView);

		// populate listview
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, activities);

		// assign adapter to list view
		activity.setAdapter(adapter);
		
		button_Back =(Button)findViewById(R.id.button_back);
		button_Back.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent BackIntent = new Intent(v.getContext(), AdminToolsActivity.class);
				startActivityForResult(BackIntent, 0);
				//add init components
				
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_summary, menu);
		return true;
	}
	
	 

	public void activitySummary() {
		try {
			// call the method to fetch all activites based on JobName
			activities = timeDB.findActivityByJob(JobValue);

			// iterate the object results
			// Iterator<ClockActivity> i = activities.iterator();

			// iterate over the list of object values
			// ClockActivity act = i.next();

		} catch (IOException e) {
			Log.e("activitySummary()", "Cannot retrive activities");
			e.printStackTrace();
		}
	}

	public void JobInfo() {
		// call the method to find job by name and return a list
		List<Job> allJobs;
		allJobs = timeDB.findJobByName(JobValue);
		// iterate the object results
		Iterator<Job> JobOb = allJobs.iterator();
		// iterate over the list in the object value
		Job i = JobOb.next();
		// assign appropriate values
		jobID = i.getName();
		jobDesc = i.getDescription();
		jobAdd = i.getAddresss();
		owner = i.getCreatedby();
	}

	public void UserInfo() {
		try {
			// call method to find user by username
			User currentUser = timeDB.fetchUserByUsername(owner);
			createdBy = currentUser.getLastName() + ", "
					+ currentUser.getFirstName();
		} catch (Exception e) {
			Log.e("UserInfo()", "Cannot retrive user");
			e.printStackTrace();
		}
	}

	 
	
	private void getExtras() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			JobValue = extras.getString("JobValue");
		}
	}

}
